﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Ninject.Web;
using Ninject;
using Legazpi.Payroll.BusinessLogic.Services.Interfaces;
using Legazpi.Payroll.DataAccess.DataObjects;
using Newtonsoft.Json;
using Legazpi.Payroll.Web.Helpers;

namespace Legazpi.Payroll.Web.Webservices
{
    /// <summary>
    /// Summary description for GovTableWebsService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class UtilitiesWebsService : WebServiceBase
    {
        [Inject]
        public IPhilHealthTableService _philHealthService { get; set; }

        [Inject]
        public ISSSTableService _sssTableService { get; set; }

        [Inject]
        public IPharaphernaliaService _pharaphernaliaService { get; set; }

        #region PhilHealth Services
        [WebMethod]
        public string UpdatePH(PhilHealthObj phItem)
        {
            //string userName = UserHelper.ValidateAndGetUserName();

            var ph = _philHealthService.GetById(phItem.Id);
            bool isAdded;
            if (ph != null)
            {
                //Update
                isAdded = false;
                ph.StartRange = phItem.StartRange;
                ph.EndRange = phItem.EndRange;
                _philHealthService.Update(ph);
            }
            else
            {
                //Add New
                isAdded = true;
                ph = new PhilHealthTable
                {
                    StartRange = phItem.StartRange,
                    EndRange = phItem.EndRange
                };
                _philHealthService.Add(ph);
            }

            var status = _philHealthService.Save();
            string json = JsonConvert.SerializeObject(
                new
                {
                    Success = status,
                    IsAdded = isAdded,
                    PHItem = ph
                });
            return json;
        }
        [WebMethod]
        public string DeletePH(int Id)
        {
            //string userName = UserHelper.ValidateAndGetUserName();

            _philHealthService.Delete(Id);
            var status = _philHealthService.Save();
            string json = JsonConvert.SerializeObject(new { Success = status });
            return json;
        }
        #endregion

        #region SSS Services
        [WebMethod]
        public string UpdateSSS(SSSObj sssItem)
        {
            //string userName = UserHelper.ValidateAndGetUserName();

            var sss = _sssTableService.GetById(sssItem.Id);
            bool isAdded;
            if (sss != null)
            {
                //Update
                isAdded = false;
                sss.StartRange = sssItem.StartRange;
                sss.EndRange = sssItem.EndRange;
                _sssTableService.Update(sss);
            }
            else
            {
                //Add New
                isAdded = true;
                sss = new SSSTable
                {
                    StartRange = sssItem.StartRange,
                    EndRange = sssItem.EndRange
                };
                _sssTableService.Add(sss);
            }
            var status = _sssTableService.Save();
            string json = JsonConvert.SerializeObject(
                new
                {
                    Success = status,
                    IsAdded = isAdded,
                    SSSItem = sss
                });
            return json;
        }
        [WebMethod]
        public string DeleteSSS(int Id)
        {
            //string userName = UserHelper.ValidateAndGetUserName();

            _sssTableService.Delete(Id);
            var status = _sssTableService.Save();
            string json = JsonConvert.SerializeObject(new { Success = status });
            return json;
        }
        #endregion

        #region PharaphernaliaServices
        [WebMethod]
        public string UpdatePharaphernalia(PharaphernaliaObj ppItem)
        {
            //string userName = UserHelper.ValidateAndGetUserName();

            var pp = ppItem.Id > 0 ? _pharaphernaliaService.GetById(ppItem.Id) : null; ;
            bool isAdded;
            if (pp != null)
            {
                //Update
                isAdded = false;
                pp.Description = ppItem.Description;
                pp.Amount = ppItem.Amount;
                _pharaphernaliaService.Update(pp);
            }
            else
            {
                //Add New
                isAdded = true;
                pp = new Pharaphernalia
                {
                    Description = ppItem.Description,
                    Amount = ppItem.Amount
                };
                _pharaphernaliaService.Add(pp);
            }
            
            var status = _pharaphernaliaService.Save();
            ppItem.Id = pp.Id;
            string json = JsonConvert.SerializeObject(
                new
                {
                    Success = status,
                    IsAdded = isAdded,
                    PPItem = ppItem
                });
            return json;
        }

        [WebMethod]
        public string DeletePharaphernalia(int Id)
        {
            //string userName = UserHelper.ValidateAndGetUserName();

            _pharaphernaliaService.Delete(Id);
            var status = _pharaphernaliaService.Save();
            string json = JsonConvert.SerializeObject(new { Success = status });
            return json;
        }
        #endregion

    }

    #region Models
    public class PhilHealthObj
    {
        public int Id { get; set; }
        public double StartRange { get; set; }
        public double EndRange { get; set; }
    }

    public class SSSObj
    {
        public int Id { get; set; }
        public double StartRange { get; set; }
        public double EndRange { get; set; }
    }

    public class PharaphernaliaObj
    {
        public int Id { get; set; }
        public string Description { get; set; }
        public double Amount { get; set; }
    }
    #endregion

}
